package com.yb0os1.controller.admin;

import com.yb0os1.constant.JwtClaimsConstant;
import com.yb0os1.dto.EmployeeDTO;
import com.yb0os1.dto.EmployeeEditPasswordDTO;
import com.yb0os1.dto.EmployeeLoginDTO;
import com.yb0os1.entity.Employee;
import com.yb0os1.properties.JwtProperties;
import com.yb0os1.result.PageResult;
import com.yb0os1.result.Result;
import com.yb0os1.serve.EmployeeServe;
import com.yb0os1.utils.JwtUtil;
import com.yb0os1.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/admin/employee")
@Tag(name = "处理员工的各种操作")
public class EmployeeController {
    @Autowired
    private EmployeeServe employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    @Operation(summary = "员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    @PostMapping("/logout")
    @Operation(summary = "员工登出")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    @Operation(summary = "新增员工")
    public Result<String> addEmployee(@RequestBody EmployeeDTO employeeDTO){
        employeeService.addEmployee(employeeDTO);
        return Result.success();
    }

    @Operation(summary = "员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageSelect(String name,Integer page,Integer pageSize){
        PageResult pageResult =  employeeService.pageSelect(name,page,pageSize);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "启用、禁用员工账号")
    public Result<String> isOpenCloseAccount(@PathVariable Integer status, Long id){
        employeeService.isOpenCloseAccount(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查找员工")
    public Result<Employee> getUserById(@PathVariable Long id){
        return employeeService.getById(id);
    }

    @Operation(summary = "编辑员工信息")
    @PutMapping
    public Result<String> updateUser(@RequestBody EmployeeDTO employeeDTO){
        employeeService.updateUser(employeeDTO);
        return Result.success();
    }

    @PutMapping("/editPassword")
    @Operation(summary = "修改密码")
    public Result<String> editPassword(@RequestBody EmployeeEditPasswordDTO employeeEditPasswordDTO){
        employeeService.editPassword(employeeEditPasswordDTO);
        return Result.success();
    }
}
